package com.whut.controller;

import com.whut.entity.UserList;
import com.whut.service.IUserListService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/userList")
public class UserListController {
    private final IUserListService userListService;

    public UserListController(IUserListService userListService) {
        this.userListService = userListService;
    }

    @GetMapping("/getAllUserList")
    public ResponseEntity<List<UserList>> getAllUserList() {
        try {
            List<UserList> userList = userListService.getAllUserList();
            return ResponseEntity.ok(userList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    @GetMapping("/getUserListById")
    public ResponseEntity<UserList> getUserListById(@RequestParam int id) {
        try {
            UserList userList = userListService.getUserListById(id);
            if (userList == null) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            return ResponseEntity.ok(userList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    @GetMapping("/getUserListByUsername")
    public ResponseEntity<List<UserList>> getUserListByUsername(@RequestParam String username) {
        try {
            List<UserList> userList = userListService.getUserListByUsername(username);
            return ResponseEntity.ok(userList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    @PostMapping("/addUserList")
    public ResponseEntity<String> addUserList(@RequestBody UserList userList) {
        try {
            userListService.addUserList(userList);
            return ResponseEntity.status(HttpStatus.CREATED).body("用户信息新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("用户信息新增失败");
        }
    }

    @PutMapping("/updateUserList")
    public ResponseEntity<String> updateUserList(@RequestBody UserList userList) {
        try {
            userListService.updateUserList(userList);
            return ResponseEntity.ok("用户信息更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("用户信息更新失败");
        }
    }

    @DeleteMapping("/deleteUserList")
    public ResponseEntity<String> deleteUserList(@RequestParam int id) {
        try {
            userListService.deleteUserList(id);
            return ResponseEntity.ok("用户信息删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("用户信息删除失败");
        }
    }
}
